<!-- DeleteContract.vue -->
<template>
    <div>
        <el-card class="box-card">
            <h2>删除合同</h2>
            <el-form ref="form" :model="form">
                <el-form-item label="选择合同">
                    <el-select v-model="form.selectedContract" placeholder="请选择合同">
                        <el-option
                                v-for="contract in contracts"
                                :key="contract.id"
                                :label="contract.name"
                                :value="contract.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" @click="deleteContract">删除</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            contracts: [],
            form: {
                selectedContract: null
            }
        };
    },
    created() {
        this.fetchContracts();
    },
    methods: {
        fetchContracts() {
            this.axios.get('/api/contract')
                .then(response => {
                    this.contracts = response.data.data;
                })
                .catch(error => {
                    console.error('Error fetching contracts:', error);
                    this.$message.error('无法获取合同列表');
                });
        },
        deleteContract() {
            if (!this.form.selectedContract) {
                this.$message.error('请选择一个合同');
                return;
            }

            this.axios.delete(`/api/contract/${this.form.selectedContract}`)
                .then(() => {
                    this.$message({
                        message: '合同删除成功',
                        type: 'success'
                    });
                    this.fetchContracts(); // Refresh the list after deletion
                    this.form.selectedContract = null; // Reset the selected contract
                })
                .catch(error => {
                    console.error('Error deleting contract:', error);
                    this.$message.error('合同删除失败');
                });
        }
    }
};
</script>

<style scoped>
.box-card {
    margin: auto;
    width: 400px;
}
.el-form-item {
    margin-bottom: 20px;
}
</style>
